//
// Created by dgy on 2023/1/7.
//
#include <numeric>
#include "iostream"
#include "vector"
using namespace std;
int minOperations(vector<int> &nums,int x){
    int len=nums.size();
    if (nums[0]>x&&nums[len-1]>x)
        return -1;
    long sum=accumulate(nums.begin(),nums.end(),0);
    long remain=sum-x;
    if (remain<0)
        return -1;
    int res=INT_MAX;
    long total=0;
    int l=0,r=0;
    while (total<remain&&r<len){
        total+=nums[r++];
    }
    while (l<len&&r<=len){
        if(total==sum){
            res=min(res,len-(r-1));
        }
        total-=nums[l++];
        while (total<remain&&r<len){
            total+=nums[r++];
        }
    }
    return res==INT_MAX?-1:res;
}
